<div id="container">
<section id="content"> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">

	$(document).ready(function(){
		var listSubStr = $("#hidListSub").val();
		var arraySub = listSubStr.split(",");
		for(var i = 0; i < arraySub.length; i++){
			$("#selSubId option[value='" + arraySub[i] + "']").prop("selected","selected");
		}
	});

	function func_back(){
		$("#main_form").prop("action", "manageListCourses");
		return $("#main_form").submit();
	}

	function func_confirm(){
		var isValidName = true;
		var isValidSub = true;
		var isValidDesc = true;
		var isValidMoney = true;
		if($("#txtName").val() == ""){
			$("#nameMsg").html("(Vui lòng nhập tên khóa học)");
			isValidName = false;
		}else{
			$("#nameMsg").html("");
			isValidName = true;
		}

		if($("#txtMoney").val() == ""){
			$("#moneyMsg").html("(Vui lòng nhập học phí)");
			isValidMoney = false;
		}else{
			if(!$.isNumeric($("#txtMoney").val())){
				$("#moneyMsg").html("(Học phí phải là số)");
				isValidMoney = false;
			}else{
				$("#moneyMsg").html("");
				isValidMoney = true;
			}
		}
		
   		if($("#selSubId").val() == "" || $("#selSubId").val() == null){
   			$("#subMsg").html("(Vui lòng chọn môn học)");
   			isValidSub = false;
   		} else{
        	$("#subMsg").html("");
        	$("#hidListSub").val($("#selSubId").val());
        	isValidSub = true;
        }
        
   		if($("#txtDescript").val() == ""){
   			$("#descMsg").html("(Vui lòng nhập nội dung mô tả)");
   			isValidDesc = false;
   		} else{
        	$("#descMsg").html("");
        	isValidDesc = true;
        }

   		if(isValidName && isValidSub && isValidDesc){
   			var mydata = {};
			mydata['name'] = $("#txtName").val();
			mydata['type'] = 'add';
			$.ajax({
		        type: "POST", 
		        url: "/it_center/Courses/checkExistName/", 
		        data: mydata,
		        success: function(data){  	
			        if(data != ""){
				        $("#nameMsg").html("(" + data + ")");
				        return;
			        }else{
			        	return $("#main_form").submit();
			        }
		       	} 
	   		});
		}	
		
	}
	
</script>
<form id="main_form" method="post" action="confirmAdd">
	<input type="hidden" name="listSubs" id="hidListSub" value="<?php echo $listSubs;?>"/>
	<center>
		<H1>Thêm Mới Khóa Học</H1>
	</center>
	<table id="edit_table">
		<tr>
			<td class="edit_column" width="170">Tên khóa học</td>
			<td style="text-align: left;"><input type="text" name="name" id="txtName"
				value="<?php echo $name;?>" />
				<br/>
				<span id="nameMsg" class="errMsg"></span>	
			</td>
		</tr>
		<tr>
			<td class="edit_column" width="170">Học phí</td>
			<td style="text-align: left;"><input type="text" name="money" id="txtMoney"
				value="<?php echo $money;?>" />
				<br/>
				<span id="moneyMsg" class="errMsg"></span>	
			</td>
		</tr>
		<tr>
			<td class="edit_column">Môn học</td>
			<td>
				<select multiple="multiple" style="width: 400px; height: 100px;" name="subId" id="selSubId">
					<?php foreach ($subjects AS $subject) :?>
						<option value="<?php echo $subject['Subject']['sub_id']?>" ><?php echo $subject['Subject']['sub_name']?></option>
					<?php endforeach;?>
					
				</select>
				<br/>
				<span id="subMsg" class="errMsg"></span>	
			</td>
		</tr>
		<tr>
			<td class="edit_column" style="vertical-align: top;">Mô tả khóa học</td>
			<td style="text-align: left;">
				<textarea name="descript" id="txtDescript"
				style="width: 99%;" rows="20"><?php echo html_entity_decode($descript, ENT_QUOTES, 'utf-8');?></textarea>
				<br/>
				<br/>
				<span id="descMsg" class="errMsg"></span>	
			</td>
		</tr>
		<tr>
			<td colspan="2" style="text-align: center;">
				<button type="button" onclick="func_confirm()">Xác Nhận</button>
				<button type="button" onclick="func_back()">Quay Lại</button>
			</td>
		</tr>
	</table>
</form>
</section>
</div>

